package com.zimmur.service.impl.zone;

import javax.annotation.Resource;  
  
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.common.EnumPointRule;
import com.zimmur.dao.zone.ITopicDao;
import com.zimmur.dao.zone.ITopicPraiseDao;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.TopicPraise;
import com.zimmur.service.IUserService;
import com.zimmur.service.account.IPointLogService;
import com.zimmur.service.zone.ITopicPraiseService;

import cn.jpush.api.report.UsersResult.User;
  
  
@Service("topicPraiseService")  
public class TopicPraiseServiceImpl implements ITopicPraiseService {

	@Resource  
    private ITopicPraiseDao topicPraiseDao;
    @Resource  
    private ITopicDao topicDao;
    @Resource
    private IUserService userService;
	@Resource
    private IPointLogService pointService;
	
	@Override
	public int insertTopicPraise(TopicPraise topicPraise) {
		return topicPraiseDao.insertTopicPraise(topicPraise);
	}

	@Override
	public int deleteTopicPraise(TopicPraise topicPraise) {
		return topicPraiseDao.deleteTopicPraise(topicPraise);
	}

	@Override
	public int updateTopicPraise(TopicPraise topicPraise) {
		return topicPraiseDao.updateTopicPraise(topicPraise);
	}

	@Override
	public TopicPraise selectTopicPraise(TopicPraise topicPraise) {
		return topicPraiseDao.selectTopicPraise(topicPraise);
	}

	@Override
	@Transactional
	public int praiseWhether(TopicPraise topicPraise) {
		Integer resut=0;
		try {
			TopicPraise topicPrais = topicPraiseDao.selectTopicPraise(topicPraise);
		  
			long topicId = topicPraise.getTopicId();
	    	Topic topic = topicDao.selectByIdstopic(topicId);
	        if(null == topicPrais){
			    resut= topicPraiseDao.insertTopicPraise(topicPraise);
			    topic.setPressNum(topic.getPressNum()+1);
	        }
	        if(null !=  topicPrais && topicPrais.getStatus().equals(1)){
	        	topicPraise.setStatus(0);
	        	resut= topicPraiseDao.updateTopicPraise(topicPraise);
	        	Integer pressNum=topic.getPressNum()-1;
	        	topic.setPressNum(pressNum);
	        }
	        if(null !=  topicPrais && topicPrais.getStatus().equals(0)){
	        	topicPraise.setStatus(1);
	        	resut= topicPraiseDao.updateTopicPraise(topicPraise);
	        	Integer pressNum=topic.getPressNum()+1;
	        	topic.setPressNum(pressNum);
	        }
	        topicDao.updateStopicPressNum(topic);
	        
	        topicPressedPoint(topicPraise.getUserId(),topic.getCreateUser());
		} catch (Exception e) {
			e.getMessage();
		}
		return resut;
	}  
    /**
     * 用户话题被点赞,积分增加规则
     * @param userId
     */
	private void topicPressedPoint(long userId,long toUserId){
		if(userId>0){
			GoodsUser fromUser = userService.selectByUserId(userId);
			if(fromUser.getCategoryCode().equals("zk")){
				pointService.mainPointLog(userId, "话题点赞", EnumPointRule.ZK_PRESS, 0);
			}
			GoodsUser toUser=userService.selectByUserId(toUserId);
			if(toUser.getCategoryCode().equals("xk")){
				pointService.mainPointLog(toUserId, "话题积赞", EnumPointRule.XK_PRESS_TO, 0);
			}
		}
	}
}  
